﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.HtmlControls;
using EasyHome.Models;
using EasyHome.DAL;
using System.Data.Entity.Infrastructure;

namespace EasyHome.Controllers
{
    public class TablaDatoController : Controller
    {
        private EasyHomeContext db = new EasyHomeContext();

        #region GET: /Index
        // GET: /Default1/
        public ActionResult Index()
        {

            return View(db.TablasDatos.ToList());
        }
        #endregion

        #region GET: /Default/Details
        public ActionResult Details(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            TablaDato tabladato = db.TablasDatos.Find(id);
            if (tabladato == null)
            {
                return HttpNotFound();
            }
            return View(tabladato);
        }
        #endregion

        #region GET: /TablaGenerica/Create
        public ActionResult Create()
        {
            PopulateTablaGenericaDropDownList();
            return PartialView("Create");
        }
        #endregion

        #region POST: /Default/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "id,Descripcion,DescripcionAbreviada,TablaGenericaId,AdicionalString,AdicionalDecimal,AdicionalBoolean,AdicionalDate")] TablaDato tabladato)
        {
            if (ModelState.IsValid)
            {
                tabladato.id = Guid.NewGuid();
                db.TablasDatos.Add(tabladato);
                db.SaveChanges();
                return Json(new { success = true });
            }

            return PartialView("Create", tabladato);
        }
        #endregion

        #region GET: /Default/Edit
        public ActionResult Edit(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            TablaDato tabladato = db.TablasDatos.Find(id);
            if (tabladato == null)
            {
                return HttpNotFound();
            }
            PopulateTablaGenericaDropDownList(tabladato.TablaGenericaId);
            return PartialView("Edit", tabladato);
        }
        #endregion

        #region POST: /Default/Edit
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "id,Descripcion,DescripcionAbreviada,TablaGenericaId,AdicionalString,AdicionalDecimal,AdicionalBoolean,AdicionalDate")] TablaDato tabladato)
        {
            try
            {

                if (ModelState.IsValid)
                {
                    db.Entry(tabladato).State = EntityState.Modified;
                    db.SaveChanges();
                    return Json(new { success = true });
                }
            }
            catch (RetryLimitExceededException /* dex */)
            {
                //Log the error (uncomment dex variable name and add a line here to write a log.)
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
            }
            PopulateTablaGenericaDropDownList(tabladato.TablaGenericaId);
            return PartialView("Edit", tabladato);
        }
        #endregion

        #region GET: /Default/Delete/
        public ActionResult Delete(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            TablaDato tabladato = db.TablasDatos.Find(id);
            if (tabladato == null)
            {
                return HttpNotFound();
            }

            return PartialView("Delete", tabladato);
        }
        #endregion

        #region POST: /Default/Delete/
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(Guid id)
        {
            TablaDato tabladato = db.TablasDatos.Find(id);
            db.TablasDatos.Remove(tabladato);
            db.SaveChanges();
            return Json(new { success = true });
        }
        #endregion

        #region ArmarGrilla
        [HttpGet]
        public string Foo(Guid? id)
        {
            StringBuilder sb = new StringBuilder();
            TablaDato tabladato = db.TablasDatos.Find(id);
            db.Entry(tabladato).Collection(u => u.TablaAdicional).Load();

            sb.Append("<table id='tablaAdicionales' class='table'>");
            sb.Append("<thead>");
            sb.Append("<tr>");
            sb.Append("<th>Descripcion</th>");
            sb.Append("<th>Nro</th>");
            sb.Append("<th>Texto</th>");
            sb.Append("<th>Fecha</th>");
            sb.Append("<th>V/F</th>");
            sb.Append("<th>Número</th>");
            sb.Append("<th></th>");
            sb.Append("</tr>");
            sb.Append("</thead>");
            sb.Append("<tbody>");

            foreach (TablaAdicional adicional in tabladato.TablaAdicional)
            {
                sb.Append("<tr>");
                sb.Append("<td>" + adicional.Descripcion + "</td>");
                sb.Append("<td>" + adicional.Numero + "</td>");
                sb.Append("<td>" + adicional.AdicionalString + "</td>");
                sb.Append("<td>" + String.Format("{0:d}", adicional.AdicionalDate) + "</td>");
                sb.Append("<td>" + adicional.AdicionalBoolean + "</td>");
                sb.Append("<td>" + adicional.AdicionalDecimal + "</td>");
                sb.Append("<td>");
                sb.Append("<a class='glyphicon glyphicon-pencil' href='/TablaAdicional/Edit/" + adicional.id + "'> </a>");
                sb.Append("<a class='glyphicon glyphicon-trash' href='/TablaAdicional/Delete/" + adicional.id + "'> </a>");
                sb.Append("</td>");

                sb.Append("</tr>");
            }

            sb.Append("</tbody>");
            sb.Append("</table>");

            return sb.ToString();
        }
        #endregion

        #region Dispose
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
        #endregion

        #region PopulateTablaGenericaDDL
        private void PopulateTablaGenericaDropDownList(object selectedTablaGenerica = null)
        {
            var tablaGenericaQuery = from d in db.TablasGenericas
                                     orderby d.Descripcion
                                     select d;
            ViewBag.TablaGenericaId = new SelectList(tablaGenericaQuery, "id", "Descripcion", selectedTablaGenerica);
        }
        #endregion
    }
}